package com.coy.web.controller;

import com.coy.web.common.dto.DataResult;
import com.coy.web.common.dto.PageResult;
import com.coy.web.common.exception.BizResultCode;
import com.coy.web.feign.dto.ShopImageRequest;
import com.coy.web.feign.dto.ShopImageVO;
import com.coy.web.service.IShopImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * demo
 *
 * @author chenck
 * @date 2019/11/19 21:01
 */
@RestController
public class ShopImageController {

    @Autowired
    private IShopImageService shopImageService;

    /**
     * 查询店铺图片
     */
    @GetMapping(value = "/selectShopImage")
    public DataResult<ShopImageVO> selectShopImage(Long id) {
        return shopImageService.selectShopImage(id);
    }

    /**
     * 查询店铺图片列表
     */
    @PostMapping(value = "/selectShopImageList")
    public PageResult<ShopImageVO> selectShopImageList(@RequestBody ShopImageRequest request) {
        return shopImageService.selectShopImageList(request);
    }

    /**
     * 保存店铺图片（支持新增和修改）
     * 注：id 为空或不存在，则新增，id存在，则修改
     */
    @PostMapping("/saveOrUpdateShopImage")
    public DataResult<Long> saveOrUpdateShopImage(@RequestBody @Valid ShopImageRequest request) {
        return shopImageService.saveOrUpdateShopImage(request);
    }

    /**
     * 删除图片
     */
    @GetMapping(value = "/deleteShopImage")
    public DataResult<Long> deleteShopImage(Long id) {
        if (null == id) {
            return DataResult.error(BizResultCode.ERR_PARAM.getCode(), "请传入图片id");
        }
        return shopImageService.deleteBatchShopImage(Arrays.asList(id));
    }

    /**
     * 批量删除图片
     */
    @PostMapping(value = "/deleteBatchShopImage")
    public DataResult<Long> deleteBatchShopImage(@RequestBody List<Long> imgIdList) {
        if (CollectionUtils.isEmpty(imgIdList)) {
            return DataResult.error(BizResultCode.ERR_PARAM.getCode(), "请传入图片id");
        }
        return shopImageService.deleteBatchShopImage(imgIdList);
    }

}
